local skynet = require "skynet"
local filelog = require "filelog"
local msghelper = require "gatesvrmsghelper"
local filename = "gatesvrrequestmsg.lua"
local base = require "base"
local GatesvrRequestMsg = {}

function GatesvrRequestMsg.process(session, source, event, ...)
	local f = GatesvrRequestMsg[event] 
	if f == nil then
		filelog.sys_error(filename.." GatesvrRequestMsg.process invalid event:"..event)
		base.skynet_retpack(nil)
		return nil
	end
	f(...)
end

function GatesvrRequestMsg.gmcmd(cmd, ... )
	if cmd == "reload" then
		msghelper:event_process("lua", "cmd", "reload", ...)
		return
	end

	if cmd == "player_add_coin" then
		local requestmsg = ...
		local server = msghelper:get_server()
		local agent_session = server.tcpmng.get_agent_session(requestmsg.rid)
		if agent_session == nil or agent_session.agent == nil then
			base.skynet_retpack(nil)
			return
		end
		local status, result_data = pcall(skynet.call, agent_session.agent, "lua", "request", "gmcmd", "player_add_coin", requestmsg.rid, requestmsg)
		if not status then
			base.skynet_retpack(nil)
			return
		end
		return base.skynet_retpack(result_data)
	end

	if cmd == "agentmng" then
		local requestmsg = ...
		local server = msghelper:get_server()
		local agent_session = server.tcpmng.get_agent_session(requestmsg.rid)
		if agent_session == nil or agent_session.agent == nil then
			base.skynet_retpack(nil)
			return
		end
		local status, result_data = pcall(skynet.call, agent_session.agent, "lua", "request", "gmcmd", cmd, requestmsg.rid, requestmsg)
		if not status then
			base.skynet_retpack(nil)
			return
		end
		return base.skynet_retpack(result_data)		
	end
	
end



return GatesvrRequestMsg